package com.jf.springmvc.fileupload;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-11-04
 * Time:14:14
 */
@Controller
@RequestMapping("/fileu")
public class UploadController {
    @PostMapping("upload")
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam("name") String name, HttpServletRequest request) throws IOException {
        String fileName = file.getOriginalFilename();
        System.out.println("fileName = " + fileName);
        System.out.println("name = " + name);
        String realPath = request.getServletContext().getRealPath("/img/" + name + fileName.substring(fileName.lastIndexOf(".")));
        file.transferTo(new File(realPath));
        return "success";
    }
}
